/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.math;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class Vector2D {
    
    private Point2D pointA;
    private Point2D pointB;

    public Vector2D() {
        this(0f,0f,0f,0f);
    }

    public Vector2D(Point2D pointA, Point2D pointB) {
        this.pointA = pointA;
        this.pointB = pointB;
    }
    
    public Vector2D(float ax, float ay, float bx, float by) {
        this.pointA = new Point2D(ax, ay);
        this.pointB = new Point2D(bx, by);
    }
    
    public void invertX() {
        pointB.move(-pointB.getX(), pointB.getY());
    }
    public void invertY() {
        pointB.move(pointB.getX(), -pointB.getY());
    }
    public void invert() {
        pointB.move(-pointB.getX(), -pointB.getY());
    }
    
    public float getLength() {
        float dx = pointA.getX()-pointB.getX();
        float dy = pointA.getX()-pointB.getX();
        return (float) Math.sqrt(dx * dx + dy * dy);
    }
    
    public void normalize() {
        float l = getLength();
        pointA.setLocation(pointA.getX()/l, pointA.getY()/l);
    }
    
    public void scale(float s) {
        pointA.setLocation(pointA.getX()*s, pointA.getY()*s);
    }
    
    public void scale(float dx, float dy) {
        pointA.setLocation(pointA.getX()*dx, pointA.getY()*dy);
    }
    
    public void translate(float dx, float dy) {
        pointA.setLocation(pointA.getX()*+dx, pointA.getY()*+dy);
    }
    
    public void add(Vector2D v) {
        pointB.setLocation(pointB.getX()+v.getX(), pointB.getY()+v.getY());
    }
    
    public void addX(float x) {
        pointA.setLocation(pointA.getX()+x, pointA.getY());
    }
    public void addY(float y) {
        pointA.setLocation(pointA.getX(), pointA.getY()+y);
    }

    public void setX(float x) {
        pointA.setLocation(x, pointA.getY());
    }

    public void setY(float y) {
        pointA.setLocation(pointA.getX(), y);
    }
    
    public void set(float x,float y) {
        pointA.setLocation(x, y);
    }

    public Point2D getPointA() {
        return pointA;
    }

    public Point2D getPointB() {
        return pointB;
    }

    public void setPointA(Point2D pointA) {
        this.pointA = pointA;
    }

    public void setPointB(Point2D pointB) {
        this.pointB = pointB;
    }
    
    public float getX() {
        return pointB.getX() - pointA.getX();
    }
    public float getY() {
        return pointA.getY() - pointB.getY();
    }
    
    public float dotProduct(Vector2D other) {
        return dotProduct(this, other);
    }
    public Vector2D crossProduct(Vector2D other) {
        return crossProduct(this, other);
    }
    
    public static float dotProduct(Vector2D v1, Vector2D v2) {
        return v1.getX()*v2.getX() + v1.getY()*v2.getY();
    }
    
    public static Vector2D crossProduct(Vector2D v1, Vector2D v2) {
        return new Vector2D(
                v1.getPointA().getX()*v2.getPointA().getX(),
                v1.getPointA().getY()*v2.getPointA().getY(),
                v1.getPointB().getX()*v2.getPointB().getX(),
                v1.getPointB().getY()*v2.getPointB().getY() );
    }
    
    public float[] straightCoefficients() {
        float matrizD = getPointA().getX();
        float matrizE = getPointA().getY();
        float matrizG = getPointB().getX();
        float matrizH = getPointB().getY();
        // -- 
        float a = (matrizE) - (matrizH);
        float b = (matrizG) - (matrizD);
        float c = (matrizD * matrizH) - (matrizE * matrizG);
        if(c<0) {
            a *= -1;
            b *= -1;
            c *= -1;
        }
        return new float[] {a,b,c};
    }
    
    public String straightEquation() {
        float e[] = straightCoefficients();
        return "("+e[0]+"x) + ("+e[1]+"y) + ("+e[2]+") = 0";
    }
    
    @Override
    public String toString() {
        return "Vector2D("+pointA+","+pointB+")";
    }
}
